Can pymc3 easily use the distributions from scipy as additional dists?


In [13]:
import pymc3 as pm
import numpy as np
import scipy
import scipy.stats
import seaborn as sns
import matplotlib.pyplot as plt
from spacepy import toolbox as tb

sns.set(font_scale=1.5)

There is an interpolsted that can do it

slow?


In [12]:
sns.distplot(scipy.stats.arcsine.rvs(size=1000))


Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0x1c2b01f690>

In [15]:
X = np.linspace(0, 1, 100)
Y = scipy.stats.arcsine.pdf(X)
plt.plot(X, Y)


Out[15]:
[<matplotlib.lines.Line2D at 0x1c2bfa9d90>]

In [28]:
with pm.Model() as model:
    arcsine = pm.Interpolated('arcsine', X, Y, testval=0.5, observed=[.3, .4, .5])
    step = pm.Metropolis()
    trace = pm.sample(10000, step=step)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-9d3b19deeb13> in <module>
      1 with pm.Model() as model:
      2     arcsine = pm.Interpolated('arcsine', X, Y, testval=0.5, observed=[.3, .4, .5])
----> 3     step = pm.Metropolis()
      4     trace = pm.sample(10000, step=step)
      5 

~/miniconda3/envs/python3/lib/python3.7/site-packages/pymc3/step_methods/arraystep.py in __new__(cls, *args, **kwargs)
     53 
     54         if len(vars) == 0:
---> 55             raise ValueError('No free random variables to sample.')
     56 
     57         if not blocked and len(vars) > 1:

ValueError: No free random variables to sample.

In [25]:
pm.traceplot(trace);



In [26]:
pm.plot_posterior(trace)


Out[26]:
array([<matplotlib.axes._subplots.AxesSubplot object at 0x1c32191d50>],
      dtype=object)
/Users/balarsen/miniconda3/envs/python3/lib/python3.7/site-packages/IPython/core/events.py:88: UserWarning: constrained_layout not applied.  At least one axes collapsed to zero width or height.
  func(*args, **kwargs)
/Users/balarsen/miniconda3/envs/python3/lib/python3.7/site-packages/IPython/core/pylabtools.py:128: UserWarning: constrained_layout not applied.  At least one axes collapsed to zero width or height.
  fig.canvas.print_figure(bytes_io, **kw)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/miniconda3/envs/python3/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
    339                 pass
    340             else:
--> 341                 return printer(obj)
    342             # Finally look for special method names
    343             method = get_real_method(obj, self.print_method)

~/miniconda3/envs/python3/lib/python3.7/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
    242 
    243     if 'png' in formats:
--> 244         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    245     if 'retina' in formats or 'png2x' in formats:
    246         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

~/miniconda3/envs/python3/lib/python3.7/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    126 
    127     bytes_io = BytesIO()
--> 128     fig.canvas.print_figure(bytes_io, **kw)
    129     data = bytes_io.getvalue()
    130     if fmt == 'svg':

~/miniconda3/envs/python3/lib/python3.7/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
   2080                     orientation=orientation,
   2081                     bbox_inches_restore=_bbox_inches_restore,
-> 2082                     **kwargs)
   2083             finally:
   2084                 if bbox_inches and restore_bbox:

~/miniconda3/envs/python3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, *args, **kwargs)
    525 
    526         else:
--> 527             FigureCanvasAgg.draw(self)
    528             renderer = self.get_renderer()
    529             with cbook._setattr_cm(renderer, dpi=self.figure.dpi), \

~/miniconda3/envs/python3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in draw(self)
    384         Draw the figure using the renderer.
    385         """
--> 386         self.renderer = self.get_renderer(cleared=True)
    387         with RendererAgg.lock:
    388             self.figure.draw(self.renderer)

~/miniconda3/envs/python3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in get_renderer(self, cleared)
    397                           and getattr(self, "_lastKey", None) == key)
    398         if not reuse_renderer:
--> 399             self.renderer = RendererAgg(w, h, self.figure.dpi)
    400             self._lastKey = key
    401         elif cleared:

~/miniconda3/envs/python3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in __init__(self, width, height, dpi)
     84         self.width = width
     85         self.height = height
---> 86         self._renderer = _RendererAgg(int(width), int(height), dpi)
     87         self._filter_renderers = []
     88 

ValueError: Image size of 349x77250 pixels is too large. It must be less than 2^16 in each direction.
<Figure size 432x288 with 1 Axes>

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [2]:
class arcsin3(mc3.Continuous):
    def random(self, point=None, size=None, repeat=None):
        return scipy.stats.arcsine.rvs(size=size, loc=point)
        
    def logp(self, value):
        return scipy.stats.arcsine.logpdf(value, loc=value)

In [5]:
with mc3.Model() as model:
    a = arcsin3('arcsin', 0, testval=1)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-3f17534b89d4> in <module>
      1 with mc3.Model() as model:
----> 2     a = arcsin3('arcsin', 0, testval=1)

~/miniconda3/envs/python3/lib/python3.7/site-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
     44             total_size = kwargs.pop('total_size', None)
     45             dist = cls.dist(*args, **kwargs)
---> 46             return model.Var(name, dist, data, total_size)
     47         else:
     48             raise TypeError("Name needs to be a string but got: {}".format(name))

~/miniconda3/envs/python3/lib/python3.7/site-packages/pymc3/model.py in Var(self, name, dist, data, total_size)
    824                 with self:
    825                     var = FreeRV(name=name, distribution=dist,
--> 826                                  total_size=total_size, model=self)
    827                 self.free_RVs.append(var)
    828             else:

~/miniconda3/envs/python3/lib/python3.7/site-packages/pymc3/model.py in __init__(self, type, owner, index, name, distribution, total_size, model)
   1272             self.tag.test_value = np.ones(
   1273                 distribution.shape, distribution.dtype) * distribution.default()
-> 1274             self.logp_elemwiset = distribution.logp(self)
   1275             # The logp might need scaling in minibatches.
   1276             # This is done in `Factor`.

<ipython-input-2-e0e798924a58> in logp(self, value)
      4 
      5     def logp(self, value):
----> 6         return scipy.stats.arcsine.logpdf(value, loc=value)

~/miniconda3/envs/python3/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py in logpdf(self, x, *args, **kwds)
   1762         x = np.asarray((x - loc)/scale, dtype=dtyp)
   1763         cond0 = self._argcheck(*args) & (scale > 0)
-> 1764         cond1 = self._support_mask(x, *args) & (scale > 0)
   1765         cond = cond0 & cond1
   1766         output = empty(shape(cond), dtyp)

~/miniconda3/envs/python3/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py in _support_mask(self, x, *args)
    895     def _support_mask(self, x, *args):
    896         a, b = self._get_support(*args)
--> 897         return (a <= x) & (x <= b)
    898 
    899     def _open_support_mask(self, x, *args):

~/miniconda3/envs/python3/lib/python3.7/site-packages/theano/tensor/var.py in __bool__(self)
     89         else:
     90             raise TypeError(
---> 91                 "Variables do not support boolean operations."
     92             )
     93 

TypeError: Variables do not support boolean operations.

In [ ]:


In [ ]:


In [ ]: